package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Sjzdsy;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 数据字典索引dao类
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午2:58
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class SjzdsyDao extends SimpleHibernateTemplate<Sjzdsy, Long> {
    public List<Sjzdsy> findSjzdsy(String bm) {
        return findByCriteria(new Criterion[]{Restrictions.eq("bm", bm)});
    }

    public Page<Sjzdsy> findSjzdsyList(String mc, String bm, int pageNo, int pageSize) {
        Page<Sjzdsy> page = new Page<Sjzdsy>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        if (StringUtils.isNotEmpty(bm)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("bm", bm, MatchMode.START)});
        } else if (StringUtils.isNotEmpty(mc)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("mc", mc, MatchMode.START)});
        } else
            return this.findAll(page);
    }

    public Boolean checkUnique(Long id, Object propertyValue, String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Sjzdsy where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Sjzdsy where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue, id);
        }
    }

}
